package com.sy.interceptor;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class MyInterceptor implements HandlerInterceptor {
    /**
     * 处理器方法执行之前的操作
     * @param req 请求对象
     * @param resp 响应对象
     * @param o 处理器及其方法对象
     * @return 如果为true则继续执行后续的拦截器和控制器方法，否则不执行
     */
    @Override
    public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object o) throws Exception {
        System.out.println("进入了拦截器");
        resp.addHeader("Access-Control-Allow-Origin", req.getHeader("Origin"));
        resp.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE,PUT,PATCH");
        resp.setHeader("Access-Control-Max-Age", "0");
        resp.setHeader("Access-Control-Allow-Headers", "*");
        resp.setHeader("Access-Control-Allow-Credentials", "true");
        resp.setHeader("XDomainRequestAllowed", "1");
        if ("OPTIONS".equals(req.getMethod())) {
            resp.setStatus(200);
        }
        return true;
    }
}
